import { message } from 'antd';
import {produce} from 'immer';
import { reqAddPlan, reqAddSudent, reqFetchList, reqGetTeacher, reqUpdateStudent, reqUploadFile,reqEditPlan} from './server';

export const models = {
  name: 'teachingProgram',
  model: {
    state:{
      loading: false,
      planList: [],
      list: [1,2,3,4,5,6],
      teacherList:[],
    },
    reducers: {
      setLoading(state, payload) {
        return produce(state, draft => {
          draft.loading = payload
        })
      },
      setPlanList(state, payload) {
        // return produce(state, draft => {
        //   draft.planList = payload
        // })
        console.log(payload)
        return {...state,planList:payload}
      },
      set(state,payload){
        return{...state,...payload}
      },
      setTeacherList(state,payload){
        return{...state,teacherList : payload}
      }
    },
    effects: dispatch => {
      return {
        async fetch(params) {
          dispatch.teachingProgram.setLoading(true)
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.teachingProgram.setPlanList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.teachingProgram.setLoading(false)
          }
        },
        async addPlan(params){
          dispatch.teachingProgram.setLoading(true)
            const { msg, code} = await reqAddPlan(params)
            code === 0 && message.success("教师分配成功")
            code !== 0 && message.error(msg)
            const { msg1, code1, data} = await reqFetchList({})
            dispatch.teachingProgram.setPlanList(data)
            code !== 0 && message.error(msg1)
          dispatch.teachingProgram.setLoading(false)
        },
        async editPlan(params){
          dispatch.teachingProgram.setLoading(true)
            const { msg, code} = await reqEditPlan(params)
            code === 0 && message.success("教师分配成功")
            code !== 0 && message.error(msg)
            const { msg1, code1, data} = await reqFetchList({})
            dispatch.teachingProgram.setPlanList(data)
            code !== 0 && message.error(msg1)
            dispatch.teachingProgram.setLoading(false)
        },
        async getTeacher(params){
          const { msg, code, data} = await reqGetTeacher(params)
            // code === 0 && message.success("教师分配成功")
            code !== 0 && message.error(msg)
            code == 0 && dispatch.teachingProgram.setTeacherList(data)
        },
        async delete(params) {
          // dispatch.teachingProgram.setLoading(true)

          // 删除学生
          
          
          // dispatch.teachingProgram.fetch()
          dispatch.teachingProgram.setState()
          
        },
        async add(params) {
          //增加学生
          dispatch.teachingProgram.setLoading(true)

          
          const {res, code, msg} = await reqAddSudent(params);
          
          // 
          // dispatch.teachingProgram.fetch()
          code !== 0 ? await message.error('添加失败' + msg) : await message.success('添加成功');
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.teachingProgram.setStudentList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.teachingProgram.setLoading(false)
          }

        },
        async update(params,a) {
          // 编辑更新
          
          dispatch.teachingProgram.setLoading(true)
          const {code, data, msg} = await reqUpdateStudent(params);
          // code === 0 && dispatch.teachingProgram.fetch()
          
          
          code !== 0 ? await message.error('修改失败' + msg) : await message.success('修改成功');
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.teachingProgram.setStudentList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.teachingProgram.setLoading(false)
          }
        },
        async uploadFile(params){
          
          dispatch.teachingProgram.setLoading(true)
          const {code, data, msg} = await reqUploadFile(params);
          code !== 0 ? await message.error('上传失败' + msg) : await message.success('上传成功');
          try {
            const { msg, code, data} = await reqFetchList(params)
            code === 0 && dispatch.teachingProgram.setStudentList(data)
            code !== 0 && message.error(msg)
          } 
          finally {
            dispatch.teachingProgram.setLoading(false)
          }
        }
      }
    }
  }
}